﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using XXZPrint.Models.Entity;

namespace XXZPrint.Models.DataOperation
{
    public class C_Do_Resources
    {
        /// <summary>
        /// 获取指定每页数据行数和页数的资源数据
        /// </summary>
        /// <param name="pageSize">每页数据行数<para>默认是每页10行</para></param>
        /// <param name="pageNum">页数<para>默认第0页</para></param>
        /// <returns></returns>
        public List<Resources> GetResources(int pageSize = 10, int pageNum = 1, List<Em_Query> Selects = null)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                Expression<Func<Resources, bool>> iSeleLambda;
                if (Selects != null)
                {
                    iSeleLambda = C_BuildExpression.BuildExpression<Resources>(Selects);
                }
                else
                {
                    iSeleLambda = C_BuildExpression.True<Resources>();
                }

                return iDbContext.DbContext.Query<Resources>()
                    .Where(iSeleLambda)
                    .TakePage(pageNum, pageSize)
                    .ToList();
            }
        }

        /// <summary>
        /// 获取指定ID的资源数据对象
        /// </summary>
        /// <param name="id">ID号</param>
        /// <returns></returns>
        public Resources GetResourcesForID(int id)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<Resources>()
                    .Where(w => w.r_id == id)
                    .FirstOrDefault();
            }
        }

        public int ResEdit(Resources data)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                if (data.r_id == 0)
                {
                    data = iDbContext.DbContext.Insert<Resources>(data);
                    return data == null ? 0 : 1;
                }
                else
                {
                    return iDbContext.DbContext.Update<Resources>(data);
                }
            }
        }

        public int  ResDel(Resources data)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Delete(data);
            }
        }

        /// <summary>
        /// 获取指定条件的数据行数
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        public int GetResCount(List<Em_Query> Selects = null)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                Expression<Func<Resources, bool>> iSeleLambda;
                if (Selects != null)
                {
                    iSeleLambda = C_BuildExpression.BuildExpression<Resources>(Selects);
                }
                else
                {
                    iSeleLambda = C_BuildExpression.True<Resources>();
                }

                return iDbContext.DbContext.Query<Resources>()
                    .Where(iSeleLambda)
                    .Count();
            }
        }
    }
}